package com.example.wechatdaka.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechatdaka.entity.Clazz;
import com.example.wechatdaka.entity.UserStudent;
import com.example.wechatdaka.entity.UserTeacher;
import com.example.wechatdaka.mapper.ClazzMapper;
import com.example.wechatdaka.mapper.UserStudentMapper;
import com.example.wechatdaka.service.ClazzService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lx
 * @since 2020-11-26
 */
@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private UserStudentMapper userStudentMapper;
    @Override
    public List<UserTeacher> selectTeacherList() {
        List<UserTeacher> clazzList = clazzMapper.selectTeacherList();
        System.out.println(clazzList.toString());

        return clazzList;
    }

    @Override
    public List<Clazz> queryAllClazz() {
        return clazzMapper.queryAllClazz();
    }

    @Override
    public IPage<Clazz> queryAllClazzPage(Page page) {
        return clazzMapper.queryAllClazzPage(page);
    }

    @Override
    public void updateClazzPeopleNum() {
        List<Clazz> clazzList = clazzMapper.selectList(null);
        for (int i = 0; i < clazzList.size(); i++) {
            String id = clazzList.get(i).getId();
            QueryWrapper<UserStudent> wrapper=new QueryWrapper<>();
            wrapper.eq("lxClazzId",id);
            List<UserStudent> studentList = userStudentMapper.selectList(wrapper);
            int size = studentList.size();
            int updatePeopleNum = clazzMapper.updatePeopleNum(id, size);
            if (updatePeopleNum>0){
                System.out.println("updateClazzPeopleNum");
            }
        }
    }

    @Override
    public IPage<Clazz> queryClazzByTeacherPage(Page page, String id) {
        return clazzMapper.queryClazzByTeacherPage(page,id);
    }

    @Override
    public IPage<Clazz> queryClazzByKeywordPage(Page page, String keyword) {
        return clazzMapper.queryClazzByKeywordPage(page,keyword);
    }
}
